//
//  WKMultipleViewController.m
//  Walk
//
//  Created by cesh on 16/9/3.
//  Copyright © 2016年 cesh. All rights reserved.
//


#import "WKMultipleViewController.h"
#import "WKCardViewController.h"
#import "WKSingleViewController.h"

@interface WKMultipleViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *topLabel;
@property (weak, nonatomic) IBOutlet UIImageView *backImg;

@property (strong, nonatomic) NSMutableArray *userInfos;

@end

@implementation WKMultipleViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.topLabel setText:[NSString stringWithFormat:@"多人活动(%li)", (long)self.count]];
    self.backImg.userInteractionEnabled = YES;
    [self.backImg addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)]];
}

//设置数据

- (void)setData:(NSArray *)data {
    _data = data;

    for (NSInteger i = 0; i < data.count; i++) {
        [self.userInfos addObject:@{
                                    @"id" : data[i][@"id"],
                                    @"header" : data[i][@"headerImg"],
                                    @"name" : data[i][@"nickname"]
                                    }];
    }
    [self setupCollectionView];
}

- (NSMutableArray *)userInfos {
    if (_userInfos == nil) {
        _userInfos = [NSMutableArray array];
    }
    return _userInfos;
}
- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dislike {
    [self dislike:nil];
}
- (IBAction)dislike:(id)sender {
    [self dismissViewControllerAnimated:NO completion:^{
        [self.cardVC dislike];
    }];
}

- (void)like {
    [self like:nil];
}
- (IBAction)like:(id)sender {
    [self dismissViewControllerAnimated:NO completion:^{
        [self.cardVC like];
    }];
}

- (void)setupCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 行间距
    flowLayout.minimumLineSpacing = 12;
    // 每个item 之间的间距
    flowLayout.minimumInteritemSpacing = 32;
    
    //根据设备不一样，每行放置的item个数不一样
    NSInteger horizontalCount = (screenW == 414) ? 5 : 4;
    
    // 设置每个cell 的尺寸
    flowLayout.itemSize = CGSizeMake(50, 73);
    
    // 设置滚动方向 水平滚动
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(12, 12, screenW - 12 * 2, ((self.count - 1) / horizontalCount + 1) * 73 + 12 * (self.count - 1) / horizontalCount) collectionViewLayout:flowLayout];
    collectionV.dataSource = self;
    collectionV.delegate = self;
    collectionV.backgroundColor = [UIColor whiteColor];
    [collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collectionV.showsVerticalScrollIndicator = NO;
    
    UIView *collectionVBg = [[UIView alloc]initWithFrame:CGRectMake(0, 64, screenW, collectionV.height + 12 * 2)];
    collectionVBg.backgroundColor = [UIColor whiteColor];
    [collectionVBg addSubview:collectionV];
    
    UIImageView *shyFrog = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shyfrog"]];
    shyFrog.center = CGPointMake(screenW * 0.5, collectionVBg.bottom + 12 + shyFrog.height * 0.5);
    
    UILabel *shyFrogLabel = [[UILabel alloc]init];
    shyFrogLabel.textColor = [UIColor colorWithRed:0x9b / 255.0 green:0x9b / 255.0 blue:0x9b / 255.0 alpha:1.0];
    shyFrogLabel.font = [UIFont systemFontOfSize:12.0];
    shyFrogLabel.text = @"暂时就这么多人啦";
    [shyFrogLabel sizeToFit];
    shyFrogLabel.center = CGPointMake(screenW * 0.5, shyFrog.bottom + 6 + shyFrogLabel.height * 0.5);
    
    [self.view addSubview:shyFrog];
    [self.view addSubview:shyFrogLabel];
    [self.view addSubview:collectionVBg];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[UICollectionViewCell alloc]init];
    }
    
    cell.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 73)];
    
    //添加头像
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

    
    icon.image = [UIImage imageNamed:@"default_head"];
    [icon sd_setImageWithURL:self.userInfos[indexPath.row][@"header"] placeholderImage:[UIImage imageNamed:@"default_head"]];
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        UIImage *head = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.userInfos[indexPath.row][@"header"]]]];
//        dispatch_sync(dispatch_get_main_queue(), ^{
//            if (head != nil) {
//                icon.image = head;
//            }
//        });
//    });
    
    icon.layer.cornerRadius = icon.width * 0.5;
    icon.layer.masksToBounds = YES;
    [cell.backgroundView addSubview:icon];
    
    //添加姓名
    UILabel *name = [[UILabel alloc]init];
    name.textColor = [UIColor colorWithRed:0x4a / 255.0 green:0x4a / 255.0 blue:0x4a / 255.0 alpha:1.0];
    name.font = [UIFont systemFontOfSize:12.0];
    name.text = self.userInfos[indexPath.row][@"name"];
    name.textAlignment = NSTextAlignmentCenter;
    [name sizeToFit];
    [name setWidth:cell.backgroundView.width];
    name.center = CGPointMake(cell.backgroundView.width * 0.5, cell.height - name.height * 0.5);
    [cell.backgroundView addSubview:name];

    return cell;
}
//点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                     @"text/html",
                                                     @"image/jpeg",
                                                     @"image/png",
                                                     @"application/octet-stream",
                                                     @"text/json",
                                                     nil];
    NSDictionary *dict = @{
                           @"token" : [NSDictionary dictionaryWithContentsOfFile:userInfoPath][@"token"],
                           @"userID" : self.userInfos[indexPath.row][@"id"],
                           };
    
    [mgr GET:[NSString stringWithFormat:@"%@Frog/getUserInfo", URL] parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"个人详情 %@",responseObject);
        
            WKSingleViewController *singleVC = [[WKSingleViewController alloc]init];
            singleVC.isSuperMultiple = YES;
            singleVC.multipleVC = self;
            singleVC.cardVC = self.cardVC;
            [self presentViewController:singleVC animated:NO completion:^{
                if (self.hideBottomBtns) {
                    //创建一个白色的view遮住底部的按钮
                    UIView *bottomCoverView = [[UIView alloc]initWithFrame:CGRectMake(0, screenH - 46, screenW, 46)];
                    bottomCoverView.backgroundColor = [UIColor colorWithRed:0xf0 / 255.0 green:0xf0 / 255.0 blue:0xf0 / 255.0 alpha:1.0];
                    [singleVC.view addSubview:bottomCoverView];
                }
            }];
            singleVC.data = responseObject[@"data"];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"个人详情失败 %@", error);
    }];
}



@end
